<template>
  <div>
    <div class="headerRole">
      <el-input placeholder="请输入角色英文名" v-model="role.name" size="small" style="width: 300px;">
        <template slot="prepend">ROLE_</template>
      </el-input>

      <el-input placeholder="请输入角色名称" v-model="role.nameZh" size="small" style="width: 300px;" @keydown.enter.native="addRole">
      </el-input>

      <el-button size="small" icon="el-icon-plus" type="primary" @click="addRole">添加角色</el-button>
    </div>
    <div style="margin-top: 10px;width: 715px;">
      <el-collapse accordion @change="change" v-model="activeName">
        <el-collapse-item :title="r.nameZh" :name="r.id" v-for="(r, index) in roles" :key="index">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>可访问资源</span>
              <el-button style="float: right; padding: 3px 0;color: red;" type="text" icon="el-icon-delete" @click="doDeleteRole(r)"></el-button>
            </div>

            <div>
              <el-tree 
              :data="allMenus" 
              :props="defaultProps" 
              show-checkbox
              :default-checked-keys="selectMenus"
              node-key="id"
              ref="tree"
              >
            </el-tree>

            <div style="display: flex;justify-content: flex-end;">
               <el-button size="mini" @click="cancelUpdate">取消修改</el-button>
               <el-button size="mini" type="primary" @click="doUpdate(r.id,index)">确认修改</el-button>
            </div>

            </div>
          </el-card>
        </el-collapse-item>
      </el-collapse>
    </div>

  </div>
</template>

<script>
export default {
  data() {
    return {
      role: {
        // 和上面进行绑定折叠弹开的id
        name: '',
        nameZh: ''
      },
      roles: [],
      allMenus: [],
      // 可以和allMenus 里面的属性绑定，名字必须是一样
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      selectMenus: [],
      activeName : -1
    }
  },
  methods: {
    // 添加角色
    addRole() {
      if (this.role.name && this.role.nameZh) {
        this.postRequest('/system/basic/permiss/role', this.role).then(resp => {
          if (resp) {
            this.initRole();
            this.role.name = '';
            this.role.nameZh = '';
          }
        })
      } else {
        this.$message.error('请填写完整的角色 !');
       }
    },
    // 初始化查询角色
    initRole() {
      this.getRequest('/system/basic/permiss/').then(resp => {
        if (resp) {
          this.roles = resp;
        }
      })
    },
    // 折叠面板发生改变
    change(rid) {
      if (rid) {
        this.initAllMenus();
        this.initSelectedMenus(rid);
      }
    },
    // 初始化全部菜单
    initAllMenus() {
      this.getRequest('/system/basic/permiss/menus').then(resp => {
        if (resp) {
          this.allMenus = resp;
         }
      })
    },
    // 初始化 角色对应的菜单
    initSelectedMenus(rid) {
      this.getRequest('/system/basic/permiss/mid/'+rid).then(resp => {
        if (resp) {
          this.selectMenus = resp;
        }
       })
    },
    // 修改
    doUpdate(rid, index) {
      // 通过下标index 获取菜单
      let tree = this.$refs.tree[index];
      let selectedKeys = tree.getCheckedKeys(tree);
      console.log(selectedKeys); 
      let url = '/system/basic/permiss/?rid='+rid;
      selectedKeys.forEach(key => {
        url += '&mids=' + key
      })

      // 发送请求
      this.putRequest(url).then(resp => {
        if (resp) {
          this.initRole();
          this.activeName = -1
        }
      })
    },
    // 取消修改
    cancelUpdate() {
       this.activeName = -1
    },
    // 删除角色
    doDeleteRole(r) {
      this.$confirm('此操作将永久删除[' + r.nameZh + ']角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

        this.deleteRequest('/system/basic/permiss/role/' + r.id).then(resp => {
          if (resp) {
            this.initRole();
          }
        })
        
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  },
  mounted() {
    this.initRole();
  }
}
</script>

<style>
.headerRole {
  display: flex;
  justify-content: flex-start;
}

.headerRole .el-input {
  margin-right: 8px;
}
</style>